package com.grass.p019lv.activity;

import android.graphics.Rect;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.DateBean;
import com.androidx.p013lv.base.bean.RecommendVideoBean;
import com.androidx.p013lv.base.bean.UserAccount;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.bean.VideoBean;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.model.VideoPlayerModel;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.SpecialCalendar;
import com.androidx.p013lv.base.utils.TimeUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.player.view.ListVideoPlayer;
import com.grass.p019lv.adapter.AdapterCalendar;
import com.grass.p019lv.adapter.AdapterRecommend;
import com.grass.p019lv.databinding.ActivityTodayBinding;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import p000a.shuyu.p005a.GSYVideoManager;

/* renamed from: com.grass.lv.activity.TodayActivity */
public class TodayActivity extends BaseActivity<ActivityTodayBinding> implements View.OnClickListener, ItemClickListener, OnRefreshLoadMoreListener, ListVideoPlayer.PlayerVideoInterface {
    String HTTP_TAG = "recommendVideo";
    private AdapterRecommend adapter;
    AdapterCalendar adapterCalendar;
    Calendar calendar;
    private String countTime;
    DateBean dateBean;
    private int firstVisibleItem;
    private int lastVisibleItem;
    private LinearLayoutManager linearLayoutManager;
    private VideoPlayerModel model;
    private int perice;
    private ListVideoPlayer player;
    private int position;
    Animation translateIn;
    Animation translateOut;
    private UserAccount userAccount;
    private UserInfo userInfo;
    ListVideoPlayer videoPlayer;
    View video_mask_old;
    private int visibleCount;

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return R.layout.activity_today;
    }

    static /* synthetic */ int access$508(TodayActivity todayActivity) {
        int i = todayActivity.visibleCount;
        todayActivity.visibleCount = i + 1;
        return i;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        this.model = (VideoPlayerModel) new ViewModelProvider(this).get(VideoPlayerModel.class);
        this.userAccount = SpUtils.getInstance().getUserAccount();
        this.userInfo = SpUtils.getInstance().getUserInfo();
        ((ActivityTodayBinding) this.binding).refresh.setOnRefreshListener(this);
        ((ActivityTodayBinding) this.binding).refresh.setOnLoadMoreListener(this);
        ((ActivityTodayBinding) this.binding).ivCalendar.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.activity.TodayActivity.View$OnClickListenerC29101 */

            public void onClick(View view) {
                TodayActivity.this.showCalendar();
            }
        });
        ((ActivityTodayBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.p019lv.activity.TodayActivity.View$OnClickListenerC29112 */

            public void onClick(View view) {
                TodayActivity todayActivity = TodayActivity.this;
                todayActivity.getInfo(todayActivity.countTime, 0);
            }
        });
        AdapterRecommend adapterRecommend = new AdapterRecommend(UrlManager.getInsatance().playBasePreview(), this);
        this.adapter = adapterRecommend;
        adapterRecommend.setItemListener(this);
        this.linearLayoutManager = new LinearLayoutManager(getActivity());
        ((ActivityTodayBinding) this.binding).recycler.setLayoutManager(this.linearLayoutManager);
        ((ActivityTodayBinding) this.binding).recycler.setAdapter(this.adapter);
        ((ActivityTodayBinding) this.binding).recycler.setHasFixedSize(true);
        ((ActivityTodayBinding) this.binding).recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            /* class com.grass.p019lv.activity.TodayActivity.C29123 */

            @Override // androidx.recyclerview.widget.RecyclerView.OnScrollListener
            public void onScrollStateChanged(RecyclerView recyclerView, int i) {
                super.onScrollStateChanged(recyclerView, i);
                if (i == 0) {
                    TodayActivity.this.autoPlayVideo(recyclerView);
                }
            }

            @Override // androidx.recyclerview.widget.RecyclerView.OnScrollListener
            public void onScrolled(RecyclerView recyclerView, int i, int i2) {
                super.onScrolled(recyclerView, i, i2);
                TodayActivity todayActivity = TodayActivity.this;
                todayActivity.firstVisibleItem = todayActivity.linearLayoutManager.findFirstVisibleItemPosition();
                TodayActivity todayActivity2 = TodayActivity.this;
                todayActivity2.lastVisibleItem = todayActivity2.linearLayoutManager.findLastVisibleItemPosition();
                TodayActivity todayActivity3 = TodayActivity.this;
                todayActivity3.visibleCount = todayActivity3.lastVisibleItem - TodayActivity.this.firstVisibleItem;
                TodayActivity.access$508(TodayActivity.this);
            }
        });
        this.model.dataBuyVideo().observe(this, new Observer<BaseRes<Integer>>() {
            /* class com.grass.p019lv.activity.TodayActivity.C29134 */

            public void onChanged(BaseRes<Integer> baseRes) {
                if (baseRes.getCode() == 200) {
                    ((VideoBean) TodayActivity.this.adapter.getDataInPosition(TodayActivity.this.position)).setBuy(true);
                    if (baseRes.getData().intValue() == 1) {
                        ToastUtils.getInstance().showCorrect("購買成功");
                        TodayActivity.this.userAccount.setGold(TodayActivity.this.userAccount.getGold() - TodayActivity.this.perice);
                        SpUtils.getInstance().setUserAccount(TodayActivity.this.userAccount);
                    } else {
                        ToastUtils.getInstance().showCorrect("租賃成功");
                        TodayActivity.this.userAccount.setGold(TodayActivity.this.userAccount.getGold() - TodayActivity.this.perice);
                        SpUtils.getInstance().setUserAccount(TodayActivity.this.userAccount);
                    }
                    if (TodayActivity.this.videoPlayer != null) {
                        TodayActivity.this.videoPlayer.setIsBuy(true);
                        return;
                    }
                    return;
                }
                ToastUtils.getInstance().showSigh(baseRes.getMsg());
            }
        });
        TimeUtils.getDate();
        getInfo("", 0);
        ((ActivityTodayBinding) this.binding).transparent.setOnClickListener(this);
        ((ActivityTodayBinding) this.binding).llTransparent.setOnClickListener(this);
        ((ActivityTodayBinding) this.binding).ivLeft.setOnClickListener(this);
        ((ActivityTodayBinding) this.binding).ivRight.setOnClickListener(this);
        ((ActivityTodayBinding) this.binding).ivDown.setOnClickListener(this);
        ((ActivityTodayBinding) this.binding).tvBack.setOnClickListener(this);
        initCalendar();
    }

    public void onClick(View view) {
        if (!isOnClick()) {
            if (view.getId() == R.id.transparent || view.getId() == R.id.iv_down) {
                dismiss();
            } else if (view.getId() == R.id.iv_left) {
                this.calendar.add(2, -1);
                setCalendar();
            } else if (view.getId() == R.id.iv_right) {
                this.calendar.add(2, 1);
                setCalendar();
            } else if (view.getId() == R.id.tv_back) {
                finish();
            }
        }
    }

    public void getVideoForDate(String str) {
        this.countTime = str;
        getInfo(str, 0);
    }

    public void getInfo(String str, final int i) {
        if (i == 0) {
            if (!NetUtil.isNetworkAvailable()) {
                if (this.binding != null && ((ActivityTodayBinding) this.binding).statusLayout != null) {
                    ((ActivityTodayBinding) this.binding).statusLayout.showNoNet();
                    return;
                }
                return;
            } else if (this.binding != null && ((ActivityTodayBinding) this.binding).statusLayout != null) {
                ((ActivityTodayBinding) this.binding).statusLayout.showLoading();
            } else {
                return;
            }
        }
        HttpUtils.getInsatance().get(UrlManager.getInsatance().recommendVideo(str, i), new HttpCallback<BaseRes<RecommendVideoBean>>(this.HTTP_TAG) {
            /* class com.grass.p019lv.activity.TodayActivity.C29145 */

            public void onLvSuccess(BaseRes<RecommendVideoBean> baseRes) {
                if (TodayActivity.this.binding != null && ((ActivityTodayBinding) TodayActivity.this.binding).statusLayout != null) {
                    ((ActivityTodayBinding) TodayActivity.this.binding).statusLayout.hideLoading();
                    ((ActivityTodayBinding) TodayActivity.this.binding).refresh.finishRefresh();
                    ((ActivityTodayBinding) TodayActivity.this.binding).refresh.finishLoadMore();
                    if (baseRes.getCode() == 200) {
                        if (baseRes.getData() == null || baseRes.getData().getData() == null || baseRes.getData().getData().size() <= 0) {
                            int i = i;
                            if (i == 0) {
                                if (TodayActivity.this.adapter == null || TodayActivity.this.adapter.getData() == null || TodayActivity.this.adapter.getData().size() == 0) {
                                    ((ActivityTodayBinding) TodayActivity.this.binding).statusLayout.showEmpty();
                                } else {
                                    ToastUtils.getInstance().showWeak("您選擇日期沒有推薦視頻");
                                }
                            } else if (i == 2) {
                                ((ActivityTodayBinding) TodayActivity.this.binding).refresh.finishLoadMoreWithNoMoreData();
                            } else {
                                ToastUtils.getInstance().showWeak("已是最新視頻，上啦查看更多推薦視頻");
                            }
                        } else if (i == 0) {
                            TodayActivity.this.adapter.setData(baseRes.getData().getData());
                            ((ActivityTodayBinding) TodayActivity.this.binding).refresh.resetNoMoreData();
                        } else {
                            VideoBean videoBean = new VideoBean();
                            videoBean.setRecAt(baseRes.getData().getRecAt());
                            videoBean.setVideoId(-1);
                            baseRes.getData().getData().add(0, videoBean);
                            if (i == 2) {
                                TodayActivity.this.adapter.setDatasInEnd(baseRes.getData().getData());
                                return;
                            }
                            if (((VideoBean) TodayActivity.this.adapter.getDataInPosition(0)).getVideoId() != -1) {
                                VideoBean videoBean2 = new VideoBean();
                                videoBean2.setRecAt(((VideoBean) TodayActivity.this.adapter.getDataInPosition(0)).getRecAt());
                                videoBean2.setVideoId(-1);
                                baseRes.getData().getData().add(videoBean2);
                            }
                            TodayActivity.this.adapter.setDatasInStart(baseRes.getData().getData());
                        }
                    } else if (i == 0) {
                        ((ActivityTodayBinding) TodayActivity.this.binding).statusLayout.showError();
                    } else {
                        ToastUtils.getInstance().showWeak(baseRes.getMsg());
                    }
                }
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onPause() {
        super.onPause();
        ListVideoPlayer listVideoPlayer = this.player;
        if (listVideoPlayer != null && listVideoPlayer.getCurrentState() != 0) {
            this.player.onVideoPause();
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onResume() {
        super.onResume();
        this.userAccount = SpUtils.getInstance().getUserAccount();
        this.userInfo = SpUtils.getInstance().getUserInfo();
        ListVideoPlayer listVideoPlayer = this.player;
        if (listVideoPlayer == null) {
            AdapterRecommend adapterRecommend = this.adapter;
            if (adapterRecommend == null || adapterRecommend.getItemCount() <= 0) {
                this.adapter.setIsPlay(1);
            } else {
                autoPlayVideo(((ActivityTodayBinding) this.binding).recycler);
            }
        } else if (listVideoPlayer.getCurrentState() == 5) {
            this.player.onVideoResume();
        } else {
            this.player.startPlayLogic();
        }
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityTodayBinding) this.binding).toolbar).statusBarDarkFont(false).init();
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        super.onDestroy();
        VideoPlayerModel videoPlayerModel = this.model;
        if (videoPlayerModel != null) {
            videoPlayerModel.cancelHttp();
        }
        HttpUtils.getInsatance().cancelTag(this.HTTP_TAG);
        GSYVideoManager.releaseAllVideos();
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view, int i) {
        if (!isOnClick()) {
            VideoBean videoBean = (VideoBean) this.adapter.getDataInPosition(i);
            int videoId = videoBean.getVideoId();
            VideoPlayerModel videoPlayerModel = this.model;
            videoPlayerModel.recommendClick(videoId + "", videoBean.getRecAt());
            startVideoPlay(videoId);
        }
    }

    private void btnBuyOrLease(VideoBean videoBean, int i) {
        if (((VideoBean) this.adapter.getDataInPosition(this.position)).isBuy()) {
            VideoPlayerModel videoPlayerModel = this.model;
            videoPlayerModel.recommendClick(videoBean.getVideoId() + "", videoBean.getRecAt());
            startVideoPlay(videoBean.getVideoId());
        } else if (this.userAccount.getGold() < this.perice) {
            ToastUtils.getInstance().showSigh("金幣不足，請充值");
            ARouter.getInstance().build(Constance.VIP_CENTER_ACTIVITY).withInt("type", 1).navigation();
        } else {
            this.model.buyOrLeaseVideo(videoBean.getVideoId(), i, false, this);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void autoPlayVideo(RecyclerView recyclerView) {
        AdapterRecommend.Holder holder;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        View view = this.video_mask_old;
        if (view != null) {
            view.setVisibility(0);
        }
        int i = 0;
        for (int i2 = 0; i2 < this.visibleCount; i2++) {
            if (!(layoutManager == null || layoutManager.getChildAt(i2) == null || ((View) Objects.requireNonNull(layoutManager.getChildAt(i2))).findViewById(R.id.player) == null)) {
                View findViewById = layoutManager.getChildAt(i2).findViewById(R.id.video_mask);
                if (findViewById != null) {
                    findViewById.setVisibility(0);
                }
                ListVideoPlayer listVideoPlayer = (ListVideoPlayer) ((View) Objects.requireNonNull(layoutManager.getChildAt(i2))).findViewById(R.id.player);
                Rect rect = new Rect();
                listVideoPlayer.getLocalVisibleRect(rect);
                int height = listVideoPlayer.getHeight();
                if (rect.top == 0 && rect.bottom == height && i == 0) {
                    i++;
                    if (findViewById != null) {
                        findViewById.setVisibility(8);
                        this.video_mask_old = findViewById;
                    }
                    if (listVideoPlayer.getCurrentState() == 0 || listVideoPlayer.getCurrentState() == 7 || listVideoPlayer.getCurrentState() == 6 || listVideoPlayer.getCurrentState() == 5) {
                        listVideoPlayer.startPlayLogic();
                        this.player = listVideoPlayer;
                    }
                } else if (findViewById != null) {
                    findViewById.setVisibility(0);
                }
            }
        }
        if (!(recyclerView.canScrollVertically(1) || (holder = (AdapterRecommend.Holder) recyclerView.findViewHolderForAdapterPosition(this.adapter.getItemCount() - 1)) == null)) {
            holder.player.startPlayLogic();
            holder.video_mask.setVisibility(8);
            this.video_mask_old = holder.video_mask;
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        AdapterRecommend adapterRecommend = this.adapter;
        if (adapterRecommend == null || adapterRecommend.getData() == null || this.adapter.getItemCount() <= 0) {
            getInfo(this.countTime, 0);
        } else {
            getInfo(((VideoBean) this.adapter.getLastDataInPosition()).getRecAt(), 2);
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        AdapterRecommend adapterRecommend = this.adapter;
        if (adapterRecommend == null || adapterRecommend.getData() == null || this.adapter.getItemCount() <= 0) {
            getInfo(this.countTime, 0);
        } else {
            getInfo(((VideoBean) this.adapter.getDataInPosition(0)).getRecAt(), 1);
        }
    }

    @Override // com.androidx.p013lv.player.view.ListVideoPlayer.PlayerVideoInterface
    public void leaseVideo(ListVideoPlayer listVideoPlayer, VideoBean videoBean, int i) {
        this.videoPlayer = listVideoPlayer;
        this.position = i;
        ((VideoBean) this.adapter.getDataInPosition(i)).getVideoId();
        this.perice = ((VideoBean) this.adapter.getDataInPosition(i)).getLeasePrice();
        btnBuyOrLease(videoBean, 3);
    }

    @Override // com.androidx.p013lv.player.view.ListVideoPlayer.PlayerVideoInterface
    public void buyVideo(ListVideoPlayer listVideoPlayer, VideoBean videoBean, int i) {
        this.videoPlayer = listVideoPlayer;
        this.position = i;
        ((VideoBean) this.adapter.getDataInPosition(i)).getVideoId();
        this.perice = ((VideoBean) this.adapter.getDataInPosition(i)).getPrice();
        btnBuyOrLease(videoBean, 1);
    }

    @Override // com.androidx.p013lv.player.view.ListVideoPlayer.PlayerVideoInterface
    public void buyVip(ListVideoPlayer listVideoPlayer, VideoBean videoBean, int i) {
        if (this.userInfo.getFreeWatches() == -1) {
            VideoPlayerModel videoPlayerModel = this.model;
            videoPlayerModel.recommendClick(videoBean.getVideoId() + "", videoBean.getRecAt());
            startVideoPlay(videoBean.getVideoId());
            return;
        }
        ARouter.getInstance().build(Constance.VIP_CENTER_ACTIVITY).navigation();
    }

    @Override // com.androidx.p013lv.player.view.ListVideoPlayer.PlayerVideoInterface
    public void click(ListVideoPlayer listVideoPlayer, VideoBean videoBean, int i) {
        this.player = listVideoPlayer;
        this.position = i;
    }

    /* access modifiers changed from: package-private */
    public void initCalendar() {
        this.calendar = Calendar.getInstance();
        TextView textView = ((ActivityTodayBinding) this.binding).tvDay;
        textView.setText(this.calendar.get(1) + "年  " + (this.calendar.get(2) + 1) + "月");
        List<DateBean> calendar2 = SpecialCalendar.getInstance().getCalendar(this.calendar);
        this.dateBean = new DateBean(this.calendar.get(1), this.calendar.get(2) + 1, this.calendar.get(5));
        this.adapterCalendar = new AdapterCalendar(getActivity(), calendar2, this.dateBean);
        ((ActivityTodayBinding) this.binding).gridView.setAdapter((ListAdapter) this.adapterCalendar);
        ((ActivityTodayBinding) this.binding).gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /* class com.grass.p019lv.activity.TodayActivity.C29156 */

            @Override // android.widget.AdapterView.OnItemClickListener
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                DateBean index;
                if (!TodayActivity.this.isOnClick() && (index = TodayActivity.this.adapterCalendar.setIndex(i)) != null) {
                    TodayActivity.this.dateBean = index;
                    TodayActivity todayActivity = TodayActivity.this;
                    todayActivity.getVideoForDate(todayActivity.dateBean.toString());
                    TodayActivity.this.dismiss();
                }
            }
        });
    }

    /* access modifiers changed from: package-private */
    public void setCalendar() {
        TextView textView = ((ActivityTodayBinding) this.binding).tvDay;
        textView.setText(this.calendar.get(1) + "年  " + (this.calendar.get(2) + 1) + "月");
        this.adapterCalendar.setList(SpecialCalendar.getInstance().getCalendar(this.calendar));
    }

    public void showCalendar() {
        ((ActivityTodayBinding) this.binding).llTransparent.setVisibility(0);
        ((ActivityTodayBinding) this.binding).llCalendar.setVisibility(0);
        if (this.translateIn == null) {
            this.translateIn = AnimationUtils.loadAnimation(this, R.anim.pop_window_calendar_in);
        }
        ((ActivityTodayBinding) this.binding).llCalendar.startAnimation(this.translateIn);
    }

    public void dismiss() {
        if (this.translateOut == null) {
            Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.pop_window_calendar_out);
            this.translateOut = loadAnimation;
            loadAnimation.setAnimationListener(new Animation.AnimationListener() {
                /* class com.grass.p019lv.activity.TodayActivity.animationAnimation$AnimationListenerC29167 */

                public void onAnimationRepeat(Animation animation) {
                }

                public void onAnimationStart(Animation animation) {
                }

                public void onAnimationEnd(Animation animation) {
                    ((ActivityTodayBinding) TodayActivity.this.binding).llCalendar.setVisibility(8);
                    ((ActivityTodayBinding) TodayActivity.this.binding).llTransparent.setVisibility(8);
                }
            });
        }
        ((ActivityTodayBinding) this.binding).llCalendar.startAnimation(this.translateOut);
    }

    @Override // androidx.activity.ComponentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onBackPressed() {
        if (((ActivityTodayBinding) this.binding).llTransparent.getVisibility() == 0) {
            dismiss();
        } else {
            super.onBackPressed();
        }
    }
}
